package thread;

import entity.ChessUser;
import message.BaseMessage;
import socket.ChessServer;

import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * 服务器accept后生成的Socket和user绑定后的监听线程.
 *
 * @author 张天羿
 */
public class ClientAtServerThread extends Thread {
    private ChessUser chessUser;
    private Socket client;


    public ChessUser getUser() {
        return chessUser;
    }

    public void setUser(ChessUser chessUser) {
        this.chessUser = chessUser;
    }

    public Socket getClient() {
        return client;
    }

    public void setClient(Socket client) {
        this.client = client;
    }

    public ClientAtServerThread(Socket client) {
        super();
        this.client = client;
    }


    public void run() {
        try {
            while (true) {
                ObjectInputStream objectInputStream = new ObjectInputStream(client.getInputStream());

                BaseMessage baseMessage = (BaseMessage) objectInputStream.readObject();
                baseMessage.setSender(this);
                System.out.println("收到数据" + baseMessage);
                baseMessage.sendToServer();
            }
        } catch (Exception e) {
            e.printStackTrace();
            ChessServer.clientAtServerThreadList.remove(this);
        }
    }
}

